package ldk.app.kotlinweibo

import android.app.Activity
import android.app.Application
import android.util.Log
import com.facebook.drawee.backends.pipeline.Fresco
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.HasActivityInjector
import ldk.app.kotlinweibo.configs.Session
import ldk.app.kotlinweibo.configs.dagger.DaggerRootComponent
import ldk.app.kotlinweibo.configs.dagger.RootModule
import ldk.util.logger.Handler
import ldk.util.logger.LLog
import javax.inject.Inject
import javax.inject.Singleton

/**
 * The application for main source set
 * Created by ldkxingzhe@163.com on 2017/6/14.
 */
@Singleton
class LApplication : Application(), HasActivityInjector{
    private val TAG = "LApplication"

    @Inject
    internal lateinit var mDispatchingAndroidInjector : DispatchingAndroidInjector<Activity>

    @Inject
    internal lateinit var mSession: Session

    override fun onCreate() {
        super.onCreate()
        LLog.init(this)
        LLog.addHandler(object : Handler {
            override fun open() {
                //ignore
            }

            override fun write(p0: Int, p1: String?, p2: String?) {
                Log.v(p1, p2)
            }

            override fun flush() {
                // ignore
            }

            override fun isOpened() = true

            override fun close() {
                //igore
            }

            override fun isClosed() = false
        })
        val rootModule = RootModule(this)
        DaggerRootComponent.builder()
                .rootModule(rootModule)
                .build()
                .inject(this)
        rootModule.mSession = mSession
        Fresco.initialize(this)
    }


    override fun activityInjector(): AndroidInjector<Activity> {
        return mDispatchingAndroidInjector
    }
}